/* @(#)GSOrderSubmitConfirmAction.java    
 *
 * Project: GS
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *   Jenny Li     2006-2-16    新增
 *   Jenny Li     2006-3-14  修改提交成功后的导向页面
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 * 
 */
package com.hitrust.gs.action;

import java.util.ArrayList;
import system.LoginContext;
import com.hitrust.gs.bean.GSOrderBean;
import com.hitrust.gs.business.GSBusOrderProcesser;
import com.hitrust.gs.business.GSOrderProcessor;
import com.hitrust.gs.business.GSUniteGwProcessor;
import com.hitrust.gs.form.GSOrderSubmitForm;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.util.exception.GSException;
import com.hitrust.gs.vo.GSOrderAuthorizeSaveVO;


/**
 * 多笔申购单提交
 */
public class GSOrderSubmitConfirmAction extends GSBaseAction {

    public String execute(GSBaseForm aForm) throws Exception {
        GSOrderSubmitForm tForm = (GSOrderSubmitForm) aForm;
        String[] orderIds = tForm.getOrderIds2();
        ArrayList tList = new ArrayList();
        //取得当前操作员对象
        LoginContext context = (LoginContext) tForm.getSession().getAttribute(GSBaseConstants.LOGIN);
        if (orderIds.length > 0) {
            try {
                //组成bean对象
                GSOrderBean tOrderBean = null;
                for (int i = 0; i < orderIds.length; i++) {
                    if (orderIds[i] != null && !orderIds[i].trim().equals("")) {
                        tOrderBean = new GSOrderBean();
                        tOrderBean.setGs_order_tempid(orderIds[i]);
                    }
                    tList.add(tOrderBean);
                }


                GSOrderProcessor tProc = new GSOrderProcessor();
                String target = (String) tForm.getRequest().getParameter( "target");

                if ("apep".equals(target)) {
                    ArrayList newList = new ArrayList();
                    for (int index1 = 0; index1 < orderIds.length; index1++) {
                        ArrayList arylist = tProc.queryOrderDetailWithStausReconfirm(orderIds[index1]);
                        for (int j = 0; j < arylist.size(); j++) {
                            String[] tempv = (String[]) arylist.get(j);
                            GSOrderAuthorizeSaveVO vo = new GSOrderAuthorizeSaveVO();
                            vo.detailId = tempv[1];
                            vo.manufactureId = tempv[2];
                            vo.orderCount = tempv[5];
                            vo.unitPrice = tempv[14].replaceAll(",", "");
                            vo.providerId = tempv[17];
                            vo.orderId = tempv[0];
                            vo.isTradeFlatManuf = tempv[19];
                            vo.isTradeFlatProvider = tempv[20];
							vo.deliverDate = tempv[6];
							vo.deliverTime = tempv[23];
							vo.productCode = tempv[4];
                            newList.add(vo);
                        }
                    }
                    //执行提交
                    GSBusOrderProcesser tBOProc = new GSBusOrderProcesser();
                    String msg= tBOProc.orderSubmitNewToApep(context, newList,null);
                    aForm.getRequest().setAttribute("message", msg);
                    //以上未提交到采购网代码
                } else {//提交到物资公司
                    //执行订单提交
                    boolean tFlag = tProc.submitOrders(tList, context.getLoginUserId());
                    if (tFlag) {
                        aForm.getRequest().setAttribute("message", "共" + orderIds.length + "笔申购单提交成功");
                    } else {
                        throw new Exception("共" + orderIds.length + "笔申购单提交失败");
                    }

                }
                //查询当前操作员所能操作订货单位的所有送货地址
                GSUniteGwProcessor processer = new GSUniteGwProcessor();
                ArrayList ary = processer.getWareHouseLevelName(context.getLoginWarehouseCode(),context.getLoginWarehouseCode());
                aForm.getRequest().setAttribute("organdaddr", ary);

                aForm.getRequest().setAttribute("requery", "true");
                return GSBaseConstants.FORWARD_SUCCESS;
            } catch (Exception e) {
                GSLogWriter.error(this, e.toString());
                aForm.getRequest().setAttribute("message", "申购提交失败," + e.toString());
                aForm.getRequest().setAttribute("action", "A100301.do");
                throw e;
            }
        } else {
            GSLogWriter.error(this, "传入的订单编号为空");
            aForm.getRequest().setAttribute("action", "A100301.do");
            aForm.getRequest().setAttribute("message", "传入的订单编号为空");
            throw new GSException(GSException.E00003, GSException.E00003_MSG);
        }
    }

}